<?php
class StudentAction extends CommonAction {
	
	/**
	 * 删除
	 * @param unknown_type $id
	 */
	public function del() {
		$result = false;
		$Student = D ( "Student" );
		
		$ids = $_POST['ids'];
		$data["XS_ZT"]  = 'xszt2'; //表明已删除
		$arrayIds = explode(',',$ids);
		
		$stu_names = '';
		$Student->startTrans();
		foreach ($arrayIds as $id){
			$xs_bh = $Student->where('Id='.$id)->field('XS_XH,XS_XM')->select(); //获取学号
			
			$order = D ("Order");
			$total = 0;
			$total = $order->where("DD_KHBH = '".$xs_bh[0]['XS_XH']."'")->count (); // 计算总数
			if($total == 0){
				//$result = $Student->where('Id='.$id)->save($data);
				$result = $Student->where('Id='.$id)->delete();
			}else{
				$stu_names = $stu_names.$xs_bh[0]['XS_XM'].",";
			}	
		}
		
		if($result){
			$Student->commit();
		}else{
			$Student->rollback();
		}
		$stu_names = substr($stu_names,0,strlen($stu_names)-1);
		$errmsg = "学生    ".$stu_names." 目前已经报名，无法删除!"; 
		
		if ($result == false) {
			echo json_encode (array(
					'errorMsg' => $errmsg
			));
		} else {
			echo json_encode (array(
					'success' => true 
			));
		}
	}
	
	/**
	 * 修改
	 * @param 
	 */
	public function save() {
		$result = false;
		$id = $_GET["id"];
		$Student = M ("Student");
		if($Student->create()) {
			$result = $Student->where('id='.$_REQUEST['id'] )->save();
			if($result) {
				echo json_encode (array('success'=>true));
			}else{
				echo json_encode (array('msg'=>'修改出错！'));
			}
		}else{
			$this->error($Student->getError());
		}
	}
	
	/**
	 * 新增
	 */
	public function add() {
		$result = false;
		$Student = D("Student");
		if($Student->create()) {
			$result = $Student->add(); 
			if($result) {
				echo json_encode (array('success'=>true));
			}else{
				echo json_encode (array('msg'=>'添加出错！'));
			}
		}else{
			$this->error($Student->getError());
		}

	}

	
	// 文件上传
	public function uploadFile() {
			header("Content-Type:text/html; charset=utf-8");
			Vendor('Excel.reader'); // 加载reader类。
			if(!$this->isPost()){
				$this->display();
			}else{
				if($_FILES['fileField']['name']!=''){
					$tmp_file=$_FILES['fileField']['tmp_name'];
					$file_types=explode(".",$_FILES['fileField']['name']);
					$file_type=$file_types[count($file_types)-1];
					if(strtolower($file_type)!="xls"){
						//$this->assign('jumpUrl',__URL__/index);
						$msg="格式错误，请重新上传";
						$this->success($msg);
					}
					$savePath="./Public/Uploads/";
					$str = date('Ymdhis');
					$file_name=$str.".".$file_type;
					if(!copy($tmp_file,$savePath.$file_name)){
						$msg="上传错误，请重新上传";
						$this->success($msg);		
					}else{
						$XS_XQ = "HKXQ";
						if(isset($_SESSION['xqid'])) {
							$XS_XQ =$_SESSION['xqid'];
						}
						$xs_fzr = $_SESSION['loginUserName'];
						
						$data = new Spreadsheet_Excel_Reader();
						$data->setOutputEncoding('utf-8');
						$data->read("./Public/Uploads/".$file_name);
						$counts = 0;
						for ($i = 2; $i <= $data->sheets[0]['numRows']; $i++) {
							$DictionaryList = D("DictionaryList");
							
							$Students = D("Student");
							$XS_XM = $data->sheets[0]['cells'][$i][1];
							$xs_xb_excel = $data->sheets[0]['cells'][$i][2];							
							$XS_CSRQ = $data->sheets[0]['cells'][$i][3];
							$xs_xx_excel = $data->sheets[0]['cells'][$i][4];
							$xs_nj_excel = $data->sheets[0]['cells'][$i][5];
							$xs_bj_excel = $data->sheets[0]['cells'][$i][6];
							
							$XS_JZXM = $data->sheets[0]['cells'][$i][7];
							$XS_JZDH = $data->sheets[0]['cells'][$i][8];
							$XS_JTDH = $data->sheets[0]['cells'][$i][9];
							$XS_JZMAIL = $data->sheets[0]['cells'][$i][10];
							$XS_JTZZ = $data->sheets[0]['cells'][$i][11];
							$XS_TJR = $data->sheets[0]['cells'][$i][12];

							$XS_NJ = $DictionaryList->getDictionaryName('JDNJ',$xs_nj_excel);//根据年级名称获取年级 nj1
							$nj_nf = substr($XS_NJ,2,strlen($XS_NJ)); //根据年级获取入学日期
							$XS_RXRQ = $Students->getRxnfForImport($nj_nf); //入学年份
								
							$XS_XH = $Students->getXhForImport($XS_XQ,$XS_RXRQ); //学号
							$XS_XB = $Students->getXbImport($xs_xb_excel); //性别
							
							$XS_PY = $Students->getpyForImport($XS_XM);//获取拼音
							$XS_PYSX = $Students->getpysxForImport($XS_XM);//获取拼音缩写
							$XS_XX = $DictionaryList->getDictionaryName('XX',$xs_xx_excel);//根据年级名称获取学校 xx001
							$XS_BJ = $DictionaryList->getDictionaryName('JDBJ',$xs_bj_excel);//根据班级名称获取年级 bj1

							$info=array(
									'XS_XH'=>$XS_XH,
									'XS_XM'=>$XS_XM,
									'XS_PY'=>$XS_PY,
									'XS_PYSX'=>$XS_PYSX,
									'XS_XB'=>$XS_XB,
									'XS_CSRQ'=>$XS_CSRQ,
									'XS_RXRQ'=>$XS_RXRQ,
									'XS_XQ'=>$XS_XQ,
									'XS_XX'=>$XS_XX,
									'XS_NJ'=>$XS_NJ,
									'XS_BJ'=>$XS_BJ,
									'XS_JZXM'=>$XS_JZXM,
									'XS_JZDH'=>$XS_JZDH,
									'XS_JTDH'=>$XS_JTDH,
									'XS_JZMAIL'=>$XS_JZMAIL,
									'XS_JTZZ'=>$XS_JTZZ,
									'XS_TJR'=>$XS_TJR,
									'XS_FZR'=>$xs_fzr,
									'XS_ZT'=>'xszt1',
							);
							if(!empty($XS_XX)&&!empty($XS_NJ)&&!empty($XS_BJ)){ //判断学校 年级 班级是否存在
								$Students->create($info,2);
								$Students->add();
								$counts++;
							}
 						}
						//$this->assign('jumpUrl',__URL__);
						$msg="学生信息导入成功,共导入了".$counts.'个学生！';
						$this->success($msg);
					}
				}
			}
	}
	
	/**
	 * 查询(non-PHPdoc)
	 * @see CommonAction::read()
	 */
	public function read() {
		$map = "  1=1 ";
		if(!empty($_POST['XS_XM'])) {
			$map = $map ." and student.XS_XM like '%".$_POST['XS_XM']."%'";
		}
		if(!empty($_POST['XS_PY'])) {
			$map = $map." and student.XS_PY like '%".$_POST['XS_PY']."%'";
			$map = $map." or student.XS_PYSX like '%".$_POST['XS_PY']."%'";
		}
		if(!empty($_POST['XS_XH'])) {
			$map = $map." and student.XS_XH like '%".$_POST['XS_XH']."%'";
		}
		if(!empty($_POST['XS_XX'])) {
			$map = $map." and student.XS_XX = '".$_POST['XS_XX']."'";
		}
		
		$map = $map." and student.XS_ZT = 'xszt1' ";
		
		$xqid =  $_SESSION['XqFilterString'] ;//获取用户当前拥有校区权限
		if(!empty($xqid)){
			$map = $map." and student.XS_XQ in (".$xqid.") ";
		}
				
		if((!empty($_POST['XS_CSRQ_BEGIN']))&&(!empty($_POST['XS_CSRQ_END']))) {
			$map = $map." and student.XS_CSRQ between '".$_POST['XS_CSRQ_BEGIN']."' and '".$_POST['XS_CSRQ_END']."'";
		}
		$temp = $_POST ['rows'];
		$pagenum = isset ( $_POST ['page'] ) ? intval ( $_POST ['page'] ) : 1;
		$rowsnum = isset ( $_POST ['rows'] ) ? intval ( $_POST ['rows'] ) : 20;
		$Student = M ( "Student" );

		$arrNum = $Student->query("select count(*) as num from smis_student student where 1=1 and " . $map);
		$row = $arrNum[0]['num'];
		$total = $row;

		$studentList = array ();
		$studentList = $Student->table('smis_student student')
								->join('smis_dictionary_list dictionary  on student.XS_NJ = dictionary.ITEM_KEY')
								->join('smis_dictionary_list dictionary1 on student.XS_XX = dictionary1.ITEM_KEY')
								->join('smis_dictionary_list dictionary2 on student.XS_BJ = dictionary2.ITEM_KEY')
								->where($map)
								->field('student.*,dictionary.ITEM_VALUE as NJ,dictionary1.ITEM_VALUE as XX,dictionary2.ITEM_VALUE as BJ')
								->order('student.id desc' )
								->page($pagenum,$rowsnum)->select();
		
		$resultJson = json_encode($studentList);
		if($resultJson == "null"){//如果查询结果返回为null
			$resultJson = "[]";
		}
		$json = '{"total":' . $total . ',"rows":' . $resultJson . '}'; // 重要，easyui的标准数据格式，数据总数和数据内容在同一个json中
		echo $json;
	}
	
	/**
	 * 查询(non-PHPdoc)
	 * @see CommonAction::read()
	public function read() {
		if(!empty($_POST['XS_XM'])) {	
			$map['XS_XM'] = array('like',"%".$_POST['XS_XM']."%");
		}
		if(!empty($_POST['XS_PY'])) {
			$map['XS_PY'] = array('like',"%".$_POST['XS_PY']."%");
		}		
		if(!empty($_POST['XS_XH'])) {
			$map['XS_XH'] = array('like',"%".$_POST['XS_XH']."%");
		}
		if(!empty($_POST['XS_XX'])) {
			$map['XS_XX'] = $_POST['XS_XX'];
		}
		$begin = $_POST['XS_CSRQ_BEGIN'];
		if((!empty($_POST['XS_CSRQ_BEGIN']))&&(!empty($_POST['XS_CSRQ_END']))) {
			$map['XS_CSRQ'] = $map['id']  = array('between',array($_POST['XS_CSRQ_BEGIN'],$_POST['XS_CSRQ_END']));
		}
		$pagenum = isset ( $_POST ['page'] ) ? intval ( $_POST ['page'] ) : 1;
		$rowsnum = isset ( $_POST ['rows'] ) ? intval ( $_POST ['rows'] ) : 10;
		$Student = M ( "Student" );
		$fix = C( "DB_PREFIX" );
		
		$total = $Student->where($map)->count (); // 计算总数	
		import("ORG.Util.Page");// 导入分页类
		$studentList = array ();
		$studentList = $Student->where($map)->limit ( ($pagenum - 1) * $rowsnum . ',' . $rowsnum )->order ( 'id asc' )->select ();
		$resultJson = json_encode($studentList);
		if($resultJson == "null"){//如果查询结果返回为null
			$resultJson = "[]";
		}
		$json = '{"total":' . $total . ',"rows":' . $resultJson . '}'; // 重要，easyui的标准数据格式，数据总数和数据内容在同一个json中		
		echo $json;
	}	 */
	
	/**
	 * 获取所有学校列表
	 */
	public function schoolList() {
		$type = $_GET["type"];
		$DictionaryList = M ( "DictionaryList" );
		$schoolList = array();
		$schoolList = $DictionaryList->where('ITEM_FLID = ' . $type)->select();
		$resultJson = json_encode($schoolList);
		$resultJson = substr($resultJson, 1, strlen($resultJson));
		$resultJson = '[{"ITEM_KEY":"0","ITEM_VALUE":"请选择","selected":true},' . $resultJson;
		//$schools = '[{"id":0,"text":"请选择","selected":true},{"id":1,"text":"华师一附中"},{"id":2,"text":"关山中学"},{"id":3,"text":"武汉三中"}]';
		echo $resultJson;
	}
	
	
	/**
	 * 获取字典表数据
	 */
// 	public function getDictList(){
// 		// $flValue = $_REQUEST ['flValue'];
// 		$flValue = $_GET["type"];
// 		$dicList = D( "DictionaryList" );
// 		$dictList = $dicList->getDictList($flValue);
// 		$resultJson = json_encode($dictList);
// 		$resultJson = substr($resultJson, 1, strlen($resultJson));
// 		$resultJson = '[{"ITEM_KEY":"0","text":"请选择","selected":true},' . $resultJson;
// 		echo $resultJson;
// 	}
	
	/**
	 * 获取推荐人
	 */
	public function getTJR() {
		$User = M ( "User" );
		$userList = array ();
		$userList = $User->order ( 'id asc' )->select ();
		$resultJson = json_encode($userList);
		$resultJson = substr($resultJson, 1, strlen($resultJson));
		$resultJson = '[{"account":"0","nickname":"请选择","selected":true},' . $resultJson;
		echo $resultJson;
	}
}
?>
